//
//  ViewController.swift
//  swift_drawer
//
//  Created by ccl on 16/3/28.
//  Copyright © 2016年 CC. All rights reserved.
//

import UIKit

class CCMuneManage: UIViewController {
   
    var leftMune: CCLeftMuneVC?;
    var centerMune: CCCenterVC?;
    var reightMune: CCReightVC?;
  

    // 屏幕的宽高
    let screenW = UIScreen.mainScreen().bounds.width;
    let screenH = UIScreen.mainScreen().bounds.height;
    
    // 偏移速度
    var panSpeed:CGFloat = 0.6;
    // 偏移量
    var panOffset:CGFloat = 0;
    // 最大缩放比例
    let maxScale:CGFloat = 0.7;
    // 最大偏移比例
    let maxOffset:CGFloat = 0.8;
    // 当前正在显示左侧菜单
    var showingLeftMune = false;
    // 当前正在显示右侧菜单
    var  showingReightMune = false;
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 添加view
        creatAddView();
    }
/// 添加view
    private func creatAddView(){
        
        // 创建手势
        let pan = UIPanGestureRecognizer(target: self, action: "viewDidPanGesture:");
        centerMune?.view.addGestureRecognizer(pan);
       
        // 注意这里的添加顺序, 会影响结果
        self.view.addSubview(leftMune!.view);
        self.view.addSubview(reightMune!.view);
        self.view.addSubview(centerMune!.view);

        
        leftMune?.view.hidden = true;
        centerMune?.view.hidden = false;
        reightMune?.view.hidden = true;
    
    }
  ///手势拖拽方法
     func viewDidPanGesture(panGesture: UIPanGestureRecognizer){
      // 1. 判断移动的方向
        let tranlstionPoint = panGesture.translationInView(self.view);
      // 计算偏移量
        panOffset = tranlstionPoint.x * panSpeed + panOffset;
        
        print(panOffset);
       
        if tranlstionPoint.x >= 0.8 {
            // 向右拖拽
            if showingReightMune == true {
                // 正在显示右侧菜单, 做centerMune 放大操作
                let centerX = (panGesture.view?.center.x)! + tranlstionPoint.x * panSpeed;
                panGesture.view?.center = CGPoint(x: centerX, y: (panGesture.view?.center.y)!);
                // 计算缩放. 最大缩放是 0.7 偏移量最大是 0.8
                
                let scale = (panOffset / screenW * maxOffset) * maxScale + maxScale;
                panGesture.view?.transform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale);
             
                hiddenOrShowMune(true, hiddenReight: false);
                
                print("1111111111111")

            }else if showingLeftMune == false {
                // 显示左侧菜单
                hiddenOrShowMune(false, hiddenReight: true);
//                showingLeftMune = true;
//                showingReightMune = false;
                // 计算中心点位移
                let centerX = (panGesture.view?.center.x)! + tranlstionPoint.x * panSpeed;
                panGesture.view?.center = CGPoint(x: centerX, y: (panGesture.view?.center.y)!);
                
                // 计算缩放. 最大缩放是 0.7 偏移量最大是 0.8
                let scale = 1 - (panOffset / screenW * maxOffset) * maxScale;
                panGesture.view?.transform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale);
                
                print("222222222222222")

            }
            
        }else if tranlstionPoint.x < -0.8{
           // 向左拖拽
           
            if showingLeftMune == true {
                // 左侧菜单已经显示, 做centerMune放大操作
                hiddenOrShowMune(false, hiddenReight: true);
                
                // 计算中心点位移
                let centerX = (panGesture.view?.center.x)! + tranlstionPoint.x * panSpeed;
                panGesture.view?.center = CGPoint(x: centerX, y: (panGesture.view?.center.y)!);
                // 计算缩放. 最大缩放是 0.7 偏移量最大是 0.8
                
                let scale = maxScale - (panOffset / screenW * maxOffset) * maxScale;
                print("\(scale) =======  \(panOffset)")
                panGesture.view?.transform = CGAffineTransformScale(CGAffineTransformIdentity,scale , scale);
                
                print("3333333333333")

                
            }else {
                // 显示 右侧菜单
                hiddenOrShowMune(true, hiddenReight: false);
//                showingReightMune = true;
//                showingLeftMune = false;
                // 计算中心点位移
                let centerX = (panGesture.view?.center.x)! + tranlstionPoint.x * panSpeed;
                panGesture.view?.center = CGPoint(x: centerX, y: (panGesture.view?.center.y)!);
                // 计算缩放. 最大缩放是 0.7 偏移量最大是 0.8
                let scale = 1 + (panOffset / screenW * maxOffset) * maxScale;
                panGesture.view?.transform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale);
                
                print("444444444444444")

                
            }
           

        }
        // 还原手势原点
        panGesture.setTranslation(CGPoint.zero, inView: self.view);

        if panGesture.state == UIGestureRecognizerState.Ended{
          
            //拖拽结束, 手指离开
            
           
            if  panOffset > (screenW * 0.15) && showingReightMune == false && panOffset > 0 {
                // 向右滑动过一半, 并且当前不是正在显示右侧菜单,显示左菜单
                showingLeftMune = true;
                showingReightMune = false;
                showLeftMune()
                
                print("555555555555")

               
            }
            else if panOffset < (screenW * 0.5) && showingLeftMune == true && panOffset > 0 {
               // 向右滑动不过一半, 并且当前左侧菜单正在显示, 继续显示左侧菜单
                showingLeftMune = true;
                showingReightMune = false;
                showLeftMune();
                print("66666666666666")

            
            }
            else if panOffset < -screenW * 0.15 && showingLeftMune == false && panOffset < 0{
                // 向左滑动过一半, 并且当前不是正在显示左侧菜单, 显示 右侧菜单
                showReightMune()
                showingReightMune = true;
                showingLeftMune = false
                
                print("777777777777")

                
            }
//            else if panOffset > -screenW * 0.25 && showingReightMune{
//            
//            
//            }
            else{
                // 显示中心菜单
                showingReightMune = false;
                showingLeftMune = false;
                showCenterMune()
                
                print("8888888888888")

                
            }
            //清零偏移量
            panOffset = 0;
        }
    }
    // 显示左边
    private func showLeftMune(){
        
        UIView.beginAnimations(nil, context: nil);
        
        // 缩放
        self.centerMune?.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, maxScale, maxScale);

        // 偏移
        self.centerMune?.view.frame.origin.x = screenW - (screenW * (1 - maxOffset) * maxScale);
    
       UIView.commitAnimations()
    }
    // 显示中间
    private func showCenterMune(){
        UIView.beginAnimations(nil, context: nil);
        // 缩放
        self.centerMune?.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0 , 1.0);
        // 偏移
        let centerX = screenW * 0.5;
        self.centerMune?.view.center = CGPoint(x: centerX, y: self.centerMune!.view.center.y);
        UIView.commitAnimations()

    }
    
    private func showReightMune(){
        UIView.beginAnimations(nil, context: nil);
        // 缩放
        self.centerMune?.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, maxScale, maxScale);
        // 偏移
        self.centerMune?.view.frame.origin.x = -screenW * maxScale * maxOffset;
        UIView.commitAnimations()
    }
    /// 显示或影藏
    private func hiddenOrShowMune(hiddenLeft: Bool,hiddenReight: Bool){
        self.leftMune?.view.hidden = hiddenLeft;
        self.reightMune?.view.hidden = hiddenReight;
    }
}

